﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QualityStormCommon;

namespace QualityStorm {
    public partial class FrmProducts : Form {
        Product m_Product;

        bool m_ReadOnly = false;

        public bool ReadOnly {
            get {
                return m_ReadOnly;
            }
            set {
                m_ReadOnly = value;
                SetReadOnly();
            }
        }

        private void SetReadOnly() {
            txtName.Enabled = !m_ReadOnly;
            btnOK.Enabled = !m_ReadOnly;
        }

        ConnectionContextBag m_contextBag;

        public ConnectionContextBag ContextBag {
            get {
                return m_contextBag;
            }
            set {
                m_contextBag = value;
            }
        }


        public Product Product {
            get {
                return m_Product;
            }
            set {
                m_Product = value;
                ShowData();
            }
        }

        private void ShowData() {
            if (m_Product != null) {
                txtName.Text = m_Product.Name;
            }
        }

        public FrmProducts(ConnectionContextBag bag) {
            this.m_contextBag = bag;
            InitializeComponent();
        }

        public FrmProducts() {
            InitializeComponent();
        }

        private void FrmProducts_Load(object sender, EventArgs e) {

        }

        private void btnOK_Click(object sender, EventArgs e) {
            if (txtName.Text == "") {
                MessageBox.Show("Please inform the name of the Product.", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            try {
                if (m_Product == null) {
                    m_Product = Product.CreateProduct(Guid.NewGuid());
                    m_Product.Name = txtName.Text;
                    if (m_contextBag.Folder != null) {
                        m_Product.Parent = m_contextBag.Folder.Guid;
                    } else {
                        m_Product.Parent = m_contextBag.Module.Guid;
                    }
                    m_contextBag.Context.Service.AddProduct(m_Product);
                    m_contextBag.Context.Service.SaveChanges();

                } else {
                    m_Product.Name = txtName.Text;
                    m_contextBag.Context.Service.UpdateProduct(m_Product);    
                }
                
                this.DialogResult = DialogResult.OK;
                Close();


            } catch (ObjectAlreadyCreatedException oace) {
                MessageBox.Show("There is already a Product with this name!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }



        }
    }
}
